<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>操作样式练习</title>
  <style>
    .contain {
      height: 300px;
      width: 300px;
      border: 1px solid #000;
    }
    .contain {
      background-color: skyblue;
    }

    .fatherBox {
      width: 300px;
      border: 1px solid #000;
      /* box-sizing: border-box; */
      display: flex;
      flex-direction: row;
      justify-content: space-around;
    }

    .fatherBox div {
      flex-grow: 1;
      text-align: center;
      /* border:1px solid #000; */
      box-sizing: border-box;
      text-decoration: none;
    }

    .bColor {
      background-color: slateblue;
    }

    .hidden {
      display: none;
    }


  </style>
</head>
<body>
  <div class="fatherBox">
    <div class="btn">标题一</div>
    <div class="btn">标题二</div>
    <div class="btn">标题三</div>
  </div>
  <div class="main">
    <div class="contain hidden" style="background-color: springgreen;">内容一</div>
    <div class="contain hidden" style="background-color: tomato;">内容二</div>
    <div class="contain hidden" style="background-color: violet;">内容三</div>
  </div>

  <script>
    let btn = document.querySelectorAll(".btn");
    let main = document.querySelectorAll(".main>div")
    // console.log(main);
    let lastIndex =0;
    let lastI = 0;

    btn.forEach(function(current,index){
      current.onclick = function(){
        //选中标题
        btn[lastIndex].classList.remove("bColor")
        current.classList.add("bColor")
        // console.log(main[index]);

        lastIndex = index;

        //切换内容
        main.forEach(function(current,i) {
          if(index == i) {
            main[lastI].classList.add("hidden");
            current.classList.remove("hidden");
            lastI = i;
          }
        })

        // if( current.style.backgroundColor) {
        //   // main[index].classList.remove("hidden")
        //   console.log(123);
        // }
      }
    })


    
  </script>
</body>
</html>